package com.report.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.CalculateUse;
import com.entity.Report_electri_Power;
import com.entity.TaskRecordItem;
import com.interfaces.TaskRecordItemDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.MeterDao;
import com.interfaces.Report_electri_PowerDao;
import com.interfaces.CalculateUseDao;
import com.mysql.fabric.xmlrpc.base.Array;
import com.report.entity.JsonTaskRecordReport;
import com.utils.CglibBeanCopierUtils;
import com.utils.JsonResult;

/**
 * @Title: TaskRecordReportService.java
 * @Package com.report.service
 * @author 孙梦楠
 * @date 2017年9月21日 上午9:11:27
 * @version V1.0.0
 */
@Service
public class TaskRecordReport_ele_Service {

	@Autowired
	private TaskRecordItemDao taskrecorditemdao;

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private Report_electri_PowerDao reportelectripowerdao;

	@Autowired
	private CalculateUseDao calculateusedao;

	// 通过表计查总报表 曲线?1
	@Transactional

	public JsonResult<Object[][]> recordreportLine(Integer customerid, String code, String time1, String time2,
			String status2) {

		if (customermeterdao.findByCustomerid2(customerid) == null) {
			return null;
		} else {
			String begintime = null;
			String endtime = null;
			if (time1 == null) {
				begintime = null;
			} else {
				begintime = time1 + " 00:00:00";
			}
			if (time2 == null) {
				endtime = null;
			} else {
				endtime = time2 + " 00:00:00";

			}
			Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();

			List<Report_electri_Power> taskRecordItems = reportelectripowerdao.findTaskrecorditemByMeter(meterid,
					code, begintime, endtime);

			Object[][] obj = new Object[taskRecordItems.size()][2];
			int i = 0;

			for (Report_electri_Power taskRecordItem : taskRecordItems) {
				String time = taskRecordItem.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				Date date = null;
				try {
					date = formatter.parse(time);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				obj[i][0] = date.getTime() + 28800000;
				obj[i][1] = Double.valueOf(taskRecordItem.getValue1());
				i++;
			}

			return new JsonResult<Object[][]>(681, "所有报表_曲线", obj);

		}
	}

	// 通过表计查总报表 表格
	@Transactional
	public JsonResult<List<JsonTaskRecordReport>> recordreportTable(Integer customerid, String code, String time1,
			String time2, String status2) {

		if (customermeterdao.findByCustomerid2(customerid) == null) {
			return new JsonResult<List<JsonTaskRecordReport>>(682, "用户不存在", null);
		} else {
				Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
				List<Report_electri_Power> taskRecordItems = reportelectripowerdao.findTaskrecorditemByMeter(meterid, code,
						time1, time2);
				List<JsonTaskRecordReport> taskRecordItems1 = new ArrayList<JsonTaskRecordReport>();
				for (Report_electri_Power taskRecordItem : taskRecordItems) {
					taskRecordItem.setValue1(Double.parseDouble(taskRecordItem.getValue1()) + "");
					taskRecordItem.setValue2(Double.parseDouble(taskRecordItem.getValue2()) + "");
					taskRecordItem.setValue3(Double.parseDouble(taskRecordItem.getValue3()) + "");
					taskRecordItem.setValue4(Double.parseDouble(taskRecordItem.getValue4()) + "");
					taskRecordItem.setValue5(Double.parseDouble(taskRecordItem.getValue5()) + "");
					taskRecordItem.setValue6(Double.parseDouble(taskRecordItem.getValue6()) + "");
					JsonTaskRecordReport report = new JsonTaskRecordReport();
					CglibBeanCopierUtils.copyProperties(taskRecordItem, report);
					report.setValue1(taskRecordItem.getValue1());
					report.setValue2(taskRecordItem.getValue2());
					report.setValue3(taskRecordItem.getValue3());
					report.setValue4(taskRecordItem.getValue4());
					report.setValue5(taskRecordItem.getValue5());
					report.setValue6(taskRecordItem.getValue6());
					taskRecordItems1.add(report);
				}
				return new JsonResult<List<JsonTaskRecordReport>>(682, "所有报表_表格", taskRecordItems1);
		}
	}
		
		// 给时标和终端地址 查询时标对应的列表
		
	
		// 给时标和终端地址 查询前一天和后一天的列表
		public List<Report_electri_Power> listByTimeAndTerminaladdress_daybefore(String time, String terminaladdress,
				String code, String type) {
			String dayBefore = dayBefore(time, type);
			return reportelectripowerdao.findByTimeAndTerminaladdressAndCode(dayBefore, terminaladdress, code);
		}

		// 后一天
		public List<Report_electri_Power> listByTimeAndTerminaladdress_dayafter(String time, String terminaladdress,
				String code, String type) {
			String dayAfter = dayAfter(time, type);
			return reportelectripowerdao.findByTimeAndTerminaladdressAndCode(dayAfter, terminaladdress, code);
		}

		// 删除当前时标示数及当前和后一天的用量记录
		@Transactional
		public boolean delectdata(String terminaladdress, String time, String code, String type) {

			String code_use = code + "_use";
			String dayafter = dayAfter(time, type);
			reportelectripowerdao.deletedata(terminaladdress, time, code);
			reportelectripowerdao.deletedata(terminaladdress, time, code_use);
			reportelectripowerdao.deletedata(terminaladdress, dayafter, code_use);

			return true;
		}

		// 前一天
		public String dayBefore(String time, String type) {
			Calendar c = Calendar.getInstance();
			Date date = null;
			try {
				date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			c.setTime(date);
			int day = 0;
			switch (type) {
			case "DD":
				day = c.get(Calendar.DATE);
				c.set(Calendar.DATE, day - 1);
				break;
			case "HH":
				day = c.get(Calendar.HOUR);
				c.set(Calendar.HOUR, day - 1);
				break;
			default:
				break;
			}
			String dayBefore = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
			return dayBefore;
		}

		// 后一天
		public String dayAfter(String time, String type) {
			Calendar c = Calendar.getInstance();
			Date date = null;
			try {
				date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			c.setTime(date);
			int day = 0;
			switch (type) {
			case "DD":
				day = c.get(Calendar.DATE);
				c.set(Calendar.DATE, day + 1);
				break;
			case "HH":
				day = c.get(Calendar.HOUR);
				c.set(Calendar.HOUR, day + 1);
				break;
			default:
				break;
			}
			String dayAfter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
			return dayAfter;
		}

}
